package servlet;

import DAO.UserDao;
import Utils.CookieUtils;
import bean.User;
import service.UserService;
import service.impl.UserServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet(name = "login", value = "/login")
public class login extends HttpServlet {
    private UserService userService;

    @Override
    public void init() throws ServletException {
        this.userService = new UserServiceImpl(new UserDao());
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
        if (userService.validateLogin(username, password)) {
            User user = userService.getUserByUsername(username);
            Cookie usernameCookie = new Cookie("username", username);
            response.addCookie(usernameCookie);
            request.getSession().setAttribute("user", user);
            response.sendRedirect("category");
        } else {
            User user = userService.getUserByUsername(username);
            if (user != null && user.getState() == 0) {
                request.getSession().setAttribute("wrong", "用户已注销");
            } else {
                request.getSession().setAttribute("wrong", "用户名或密码错误");
            }
            Cookie usernameCookie = new Cookie("username", username);
            response.addCookie(usernameCookie);
            response.sendRedirect("index.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cookies = request.getCookies();
        String code = CookieUtils.getCookie(cookies, "code");
        String usercode = request.getParameter("code");
        String username = request.getParameter("username");
        
        if (usercode.equals(code)) {
            String action = request.getParameter("action");
            try {
                Method method = this.getClass().getMethod(action, HttpServletRequest.class, HttpServletResponse.class);
                method.invoke(this, request, response);
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        } else {
            response.addCookie(new Cookie("username", username));
            request.getSession().setAttribute("wrong", "验证码错误");
            response.sendRedirect("index.jsp");
        }
    }
}
